package com.hc.controller;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/file")
@Api(description = "文件上传")
public class QcFileUploadController {

    @Value("${file.url}")
    private String uploadPath;


    @PostMapping(value = "/moreFileUpload", headers = "content-type=multipart/form-data")
    public String fileUpload(MultipartFile file) throws IOException {
        // 上传文件名
        String filename = file.getOriginalFilename();
        //获取当天日期
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String format1 = format.format(new Date());
        File file1 = new File(uploadPath + format1);
        if (!file1.getParentFile().exists()) {
            file1.getParentFile().mkdirs();
        }
        if (!file1.exists()) {
            file1.mkdirs();
        }
        File savefile = new File(uploadPath + format1 + "\\" + filename);
        try {
            file.transferTo(savefile);
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
        String path = savefile.getPath();
        return path.substring(10, path.length());
}
}
